﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;

namespace ReadTheRecord
{
    public class Program
    {  
        const string SampleFileDataPath1 = "C:\\Users\\Administrator\\Desktop\\测试用\\SampleFile1.data";
        const string SampleFileDataPath2 = "C:\\Users\\Administrator\\Desktop\\测试用\\SampleFile2.data";//错题统计
        public static string[] s;//取出题目
        static List<string> Topic = new List<string>();//记录题目
        static List<string> Mistake = new List<string>();//记录用户输入的答题
        static void Main(string[] args)
        {

            ReadFileUsingFileStream(SampleFileDataPath1);
            CreateSampleFileAsync(Topic.Count);
        }
        public static async Task CreateSampleFileAsync(int nRecords)
        {
            using (FileStream stream = File.Create(SampleFileDataPath2))
            using (var writer = new StreamWriter(stream))
            {
                foreach (var i in Topic)
                {
                    string c = $"{i}{Environment.NewLine}";
                    await writer.WriteAsync(c);
                }
            }
        }
        public static void ReadFileUsingFileStream(string fileName)
        {
            const int BUFFERSIZE = 3072;
            using (var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                Encoding encoding = GetEncoding(stream);
                byte[] buffer = new byte[BUFFERSIZE];
                int nread = stream.Read(buffer, 0, BUFFERSIZE);
                if (nread < BUFFERSIZE)
                {
                    Array.Clear(buffer, nread, BUFFERSIZE - nread);
                }
                s = encoding.GetString(buffer, 0, nread).Split("\r\n");//将文件中的题目拿出同时去掉换行等字符，同时将题目以数组的方式放入s
                foreach (var i in s)
                {
                    var aw = i.IndexOf("=");//将“=”所在下标位置求出。
                    Console.WriteLine("题目为：");
                    var topic = i.Remove(aw);//将题目显示出来，隐藏答案
                    Console.WriteLine(topic);//输出题目                   
                    var n = i.Substring(aw+1);//从数组中将从文件中获取的答案取出
                    var Throw = Console.ReadLine();//获取控制台输出的答案
                    var str = i.Remove(aw+1
                        ) + $"{Throw}";//将控制台输入的答案记录下来
                    Mistake.Add(Throw);//将输入进去的答案保存
                    if (n == Throw)
                    {
                        Console.WriteLine("恭喜你答对了");                       
                        Topic.Add($"{str}   √");
                    }
                    else
                    {
                        Console.WriteLine("对不起你答错了");                        
                        Topic.Add($"{str}   ×");               
                    }
                }
            }
        }
        // read BOM
        public static Encoding GetEncoding(Stream stream)
        {
            if (!stream.CanSeek) throw new ArgumentException("require a stream that can seek");

            Encoding encoding = Encoding.ASCII;

            byte[] bom = new byte[5];
            int nRead = stream.Read(bom, offset: 0, count: 5);
            if (bom[0] == 0xff && bom[1] == 0xfe && bom[2] == 0 && bom[3] == 0)
            {
                Console.WriteLine("UTF-32");
                stream.Seek(4, SeekOrigin.Begin);
                return Encoding.UTF32;
            }
            else if (bom[0] == 0xff && bom[1] == 0xfe)
            {
                Console.WriteLine("UTF-16, little endian");
                stream.Seek(2, SeekOrigin.Begin);
                return Encoding.Unicode;
            }
            else if (bom[0] == 0xfe && bom[1] == 0xff)
            {
                Console.WriteLine("UTF-16, big endian");
                stream.Seek(2, SeekOrigin.Begin);
                return Encoding.BigEndianUnicode;
            }
            else if (bom[0] == 0xef && bom[1] == 0xbb && bom[2] == 0xbf)
            {
                Console.WriteLine("UTF-8");
                stream.Seek(3, SeekOrigin.Begin);
                return Encoding.UTF8;
            }
            stream.Seek(0, SeekOrigin.Begin);
            return encoding;
        }


    }
}
